package com.qf.ts.platform.front.web;

import com.qf.ts.platform.commons.bean.User;
import com.qf.ts.platform.commons.constant.SystemParams;
import com.qf.ts.platform.commons.pojo.R;
import com.qf.ts.platform.front.service.UserService;
import com.qf.ts.platform.front.util.AliyunOssUtil;
import com.qf.ts.platform.front.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@Controller
@RequestMapping("/user")
@Api(value = "用于处理关于用户的相关请求")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AliyunOssUtil aliyunOssUtil;

    @PostMapping("/show")
    @ApiIgnore
    public String o(HttpSession session){

        session.setAttribute("role",2);
        session.setAttribute("player",3);

        return "upload";
    }

    @GetMapping("/")
    @ResponseBody
    @ApiOperation(value = "查询用户是否存在", notes = "本接口是用来验证用户登录...")
    @ApiImplicitParam(name = "user", value = "传递的用户实体类", required = true, dataTypeClass = User.class)
    public R userLogin(User user){

        //这里使用shiro进行验证拦截

        Subject subject = SecurityUtils.getSubject();
//        System.out.println("传入的pwd---" + user.getPassword());
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), MD5Util.md5(user.getPassword()));

        try {
            System.out.println(new String(token.getPassword()));
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return R.error("error");
        }

        if (subject.isAuthenticated()) {
            return R.ok();
        } else {
            return R.error("error");
        }


//        Integer result = userService.findUser(user);
//
//        if (result > 0 ){
//
//            return new R(true,"success");
//
//        }
//
//        return new R(false,"success");
    }

    @PostMapping("/")
    @ResponseBody
    @ApiOperation(value = "用户注册", notes = "本接口是用来进行用户信息注册,同时包含图片上传功能...")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "user", value = "传递的用户实体类", required = true, dataTypeClass = User.class),
            @ApiImplicitParam(name = "code", value = "需要提交而来的验证码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "file", value = "需要前端页面传输来的图片文件", required = false, dataTypeClass = MultipartFile.class),
    })
    public R userRegister( User user, String code, HttpSession session,@RequestParam("file") MultipartFile file){

        System.out.println("进入了register");
//        if(!code.equals("2233")){
//
//            return new R(false,"验证码错误");
//
//        }

        Integer role = (Integer)session.getAttribute("role");
        Integer player = (Integer)session.getAttribute("player");
        user.setRole(role);
        user.setPlayer(player);
        //TODO: 判断role,如果为企业用户,则上传图片,且图片不得为空
        System.out.println("进入了判断环节");

        try{
            if(role == SystemParams.ROLE_COMPANY ){

                String filename = file.getOriginalFilename();
                if (!"".equals(filename.trim())) {
                    File destFile = new File(filename);
                    FileOutputStream fos = new FileOutputStream(destFile);
                    fos.write(file.getBytes());
                    fos.close();
                    file.transferTo(destFile);

                    //开始上传到oos

                    String path = aliyunOssUtil.upload(destFile);
                    System.out.println("path=" + path);
                    user.setImg(path);

                    session.setAttribute("url", path);
                }

            }
        }catch (IOException e){
            e.printStackTrace();
        }

        Integer result = userService.addUser(user);

        if(result > 0){

            return new R(true,"success");
        }

        return new R(false,"fail");

    }


}
